/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package resource.store;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;

import resource.models.Board;
import resource.models.Creature;
import resource.models.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * REST Web Service
 *
 * @author rswain
 */
@Path("DataStore")
public class DataStoreResource implements DataStoreInterface {

    @Context
    private UriInfo context;

    public DataStoreResource(){
		
	}
	
	@Override
        @GET
        @Produces("application/json")
	public Board[] getBoards() {
            File currentDirectory = new File(".");    
            
            String[] fileNames = currentDirectory.list();
            
            ArrayList<Board> boards = new ArrayList<Board>();
            
            for(String name : fileNames){
                if(name.contains(".board")){
                    try{
			FileInputStream fis = new FileInputStream(name); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			Board board = (Board) ois.readObject(); 
			ois.close();
                        boards.add(board);
                    }catch(Exception e){
                        System.out.println("Couldn't get a board.");
                    }
                }
            }
            
            return (Board[]) boards.toArray();
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("application/json")
	public boolean saveBoard(Board b) {
		try {
			
			String fname = b.getName() + ".board";
			
			File test = new File(fname);
			if(test.exists()){
				test.delete();
			}
			
			FileOutputStream fos = new FileOutputStream(fname); 
			ObjectOutputStream oos = new ObjectOutputStream(fos); 
			oos.writeObject(b);
			oos.flush(); 
			oos.close(); 
		
			return true;
		} catch (IOException e) {
			System.out.println("SORRY, couldn't save the board!");
			return false;
		} 
	}

	@Override
        @GET
        @Produces("application/json")
	public Creature[] getCreatures() {
            File currentDirectory = new File(".");    
            
            String[] fileNames = currentDirectory.list();
            
            ArrayList<Creature> creatures = new ArrayList<Creature>();
            
            for(String name : fileNames){
                if(name.contains(".creature")){
                    try{
			FileInputStream fis = new FileInputStream(name); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			Creature creature = (Creature) ois.readObject(); 
			ois.close();
                        creatures.add(creature);
                    }catch(Exception e){
                        System.out.println("Couldn't get a Creature.");
                    }
                }
            }
            
            return (Creature[]) creatures.toArray();
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("application/json")
	public boolean storeCreature(Creature c) {
		try {
			
			String fname = c.getName() + ".creature";
			
			File test = new File(fname);
			if(test.exists()){
				test.delete();
			}
			
			FileOutputStream fos = new FileOutputStream(fname); 
			ObjectOutputStream oos = new ObjectOutputStream(fos); 
			oos.writeObject(c);
			oos.flush(); 
			oos.close(); 
		
			return true;
		} catch (IOException e) {
			System.out.println("SORRY, couldn't save the creature!");
			return false;
		} 
	}

	@Override
        @GET
        @Produces("application/json")
	public Resource[] getResources() {
            File currentDirectory = new File(".");    
            
            String[] fileNames = currentDirectory.list();
            
            ArrayList<Resource> resources = new ArrayList<Resource>();
            
            for(String name : fileNames){
                if(name.contains(".resource")){
                    try{
			FileInputStream fis = new FileInputStream(name); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			Resource resource = (Resource) ois.readObject(); 
			ois.close();
                        resources.add(resource);
                    }catch(Exception e){
                        System.out.println("Couldn't get a resource.");
                    }
                }
            }
            
            return (Resource[]) resources.toArray();
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("application/json")
	public boolean storeResource(Resource r) {
		try {
			
			String fname = r.getName() + ".resource";
			
			File test = new File(fname);
			if(test.exists()){
				test.delete();
			}
			
			FileOutputStream fos = new FileOutputStream(fname); 
			ObjectOutputStream oos = new ObjectOutputStream(fos); 
			oos.writeObject(r);
			oos.flush(); 
			oos.close(); 
		
			return true;
		} catch (IOException e) {
			System.out.println("SORRY, couldn't save the resource!");
			return false;
		} 
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(test()){
			System.out.println("Everything's workin' great!");
		}else{
			System.out.println("WE'VE GOT PROBLEMS!!!!!!!!!");
		}

	}
	
	public static boolean test(){
		boolean toReturn = true;
		
		//testing board persistence
		
		/*
		DataStore testDS = new DataStore();
		Board testBoard = new Board();
		testDS.saveBoard(testBoard);
		Board[] testBoards = testDS.getBoards();
		for(Board b : testBoards){
			if(b.equals(testBoard)){
				toReturn = true;
			}
		}
		
		*/
		
		DataStoreResource testDS = new DataStoreResource();
		Board testBoard = new Board();
		testDS.saveBoard(testBoard);
		Board returned = testDS.getBoard(testBoard.getName());
		if(!testBoard.equals(returned)){
			toReturn = false;
		}
		
                //TODO - still need to test other methods
		
		
		return toReturn;
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("text/plain")
	public Board getBoard(String boardName) {
		String fname = boardName + ".board";
		
		File test = new File(fname);
		if(!test.exists()){
			return null;
		}
		
		try { 
			Board toReturn;
			FileInputStream fis = new FileInputStream(fname); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			toReturn = (Board) ois.readObject(); 
			ois.close(); 
			
			return toReturn;
			
		} catch(Exception e) { 
			System.out.println("SORRY, couldn't get " + fname + "!"); 
			return null;
		} 
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("text/plain")
	public Creature getCreature(String creatureName) {
		String fname = creatureName + ".creature";
		
		File test = new File(fname);
		if(!test.exists()){
			return null;
		}
		
		try { 
			Creature toReturn;
			FileInputStream fis = new FileInputStream(fname); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			toReturn = (Creature) ois.readObject(); 
			ois.close(); 
			
			return toReturn;
			
		} catch(Exception e) { 
			System.out.println("SORRY, couldn't get " + fname + "!"); 
			return null;
		} 
	}

	@Override
        @GET
        @Produces("application/json")
        @Consumes("text/plain")
	public Resource getResource(String resourceName) {
		String fname = resourceName + ".resource";
		
		File test = new File(fname);
		if(!test.exists()){
			return null;
		}
		
		try { 
			Resource toReturn;
			FileInputStream fis = new FileInputStream(fname); 
			ObjectInputStream ois = new ObjectInputStream(fis); 
			toReturn = (Resource) ois.readObject(); 
			ois.close(); 
			
			return toReturn;
			
		} catch(Exception e) { 
			System.out.println("SORRY, couldn't get " + fname + "!"); 
			return null;
		} 
	}
}
